#!/usr/bin/python
# -*- coding:utf-8 -*-

import os,sys
import numpy as np
import csv
from math import *

root = os.getcwd()
for parent,dirnames,filenames in os.walk(root):    #三个参数：分别返回1.父目录 2.所有文件夹名字（不含路径） 3.所有文件名字
    for dirname in  dirnames:                       #输出文件夹信息
        print "parent is:" + parent
        print  "dirname is" + dirname
    for filename in filenames:                        #输出文件信息
        print "parent is:" + parent
        print "filename is:" + filename
        print "the full name of the file is:" + os.path.join(parent,filename) #输出文件路径信息
        if filename.find(".fa")!=-1:
            file_path=os.path.join(parent,filename)
            f=open(file_path,'r')
            N = 100
            x1_list = []
            y1_list = []
            x2_list = []
            y2_list = []
            x3_list = []
            y3_list = []
            x4_list = []
            y4_list = []
            x5_list = []
            y5_list = []
            x6_list = []
            y6_list = []
            x7_list = []
            y7_list = []
            x8_list = []
            y8_list = []
            x9_list = []
            y9_list = []
            x10_list = []
            y10_list = []
            x11_list = []
            y11_list = []
            x12_list = []
            y12_list = []
            x13_list = []
            y13_list = []
            x14_list = []
            y14_list = []
            x15_list = []
            y15_list = []
            f.seek(0,1)
            count = 0
            temp = f.read(1)
            if temp ==">":
                temp = f.read(60)
            while True:
                temp = f.read(N)
                count_N = temp.count('N')
                count_N = count_N+temp.count("\n")
                while count_N !=0:
                    temp = temp.replace('N','')
                    temp = temp.replace("\n",'')
                    temp = temp + f.read(count_N)
                    count_N = temp.count('N')
                    count_N = count_N+temp.count("\n")
                if temp == '':
                    break
                temp = temp.upper()
                count_A = temp.count('A')
                count_T = temp.count('T')
                count_G = temp.count('G')
                count_C = temp.count('C')
                count_X1 = count_A
                count_Y1 = count_A
                count_X2 = count_T
                count_Y2 = count_T
                count_X3 = count_C
                count_Y3 = count_C
                count_X4 = count_G
                count_Y4 = count_G
                count_X5 = count_A+count_T
                count_Y5 = count_A+count_T
                count_X6 = count_A+count_C
                count_Y6 = count_A+count_C
                count_X7 = count_A+count_G
                count_Y7 = count_A+count_G
                # count_X8 = count_T+count_C
                # count_Y8 = count_T+count_C
                # count_X9 = count_T+count_G
                # count_Y9 = count_T+count_G
                # count_X10 = count_C+count_G
                # count_Y10 = count_C+count_G
                # count_X11 = count_A+count_T+count_C
                # count_Y11 = count_A+count_T+count_C
                # count_X12 = count_A+count_T+count_G
                # count_Y12 = count_A+count_T+count_G
                # count_X13 = count_A+count_C+count_G
                # count_Y13 = count_A+count_C+count_G
                # count_X14 = count_T+count_C+count_G
                # count_Y14 = count_T+count_C+count_G
                # count_X15 = count_A+count_T+count_C+count_G
                # count_Y15 = count_A+count_T+count_C+count_G
                if count_X5==0 and count_Y7==0:
                    print temp
                    print len(temp)
                x1_list.append(count_X1)
                y1_list.append(count_Y1)
                x2_list.append(count_X2)
                y2_list.append(count_Y2)
                x3_list.append(count_X3)
                y3_list.append(count_Y3)
                x4_list.append(count_X4)
                y4_list.append(count_Y4)
                x5_list.append(count_X5)
                y5_list.append(count_Y5)
                x6_list.append(count_X6)
                y6_list.append(count_Y6)
                x7_list.append(count_X7)
                y7_list.append(count_Y7)
                # x8_list.append(count_X8)
                # y8_list.append(count_Y8)
                # x9_list.append(count_X9)
                # y9_list.append(count_Y9)
                # x10_list.append(count_X10)
                # y10_list.append(count_Y10)
                # x11_list.append(count_X11)
                # y11_list.append(count_Y11)
                # x12_list.append(count_X12)
                # y12_list.append(count_Y12)
                # x13_list.append(count_X13)
                # y13_list.append(count_Y13)
                # x14_list.append(count_X14)
                # y14_list.append(count_Y14)
                # x15_list.append(count_X15)
                # y15_list.append(count_Y15)
            import matplotlib.pyplot as plt
            import mpl_toolkits.mplot3d
            from matplotlib import cm
            from matplotlib.ticker import LinearLocator, FormatStrFormatter
            r_list = []
            area = [[0 for i in range(N+1)] for m in range(N+1)]
            #for i in xrange(len(s_list)-1):
            #    a = s_list[i]*1.0/s_list[i+1]
            #    a = round(a,4)
            #    r_list.append(a)
            #print r_list
            xx = []
            yy = []


            x16 = []
            y16 = []
            for index_i in range(len(y1_list)):
                x16.append(0)
                y16.append(0)
            xx.append(x16)
            yy.append(y16)

            xx.append(x1_list)
            xx.append(x2_list)
            xx.append(x3_list)
            xx.append(x4_list)
            xx.append(x5_list)
            xx.append(x6_list)
            xx.append(x7_list)
            #xx.append(x8_list)
            #xx.append(x9_list)
            #xx.append(x10_list)
            #xx.append(x11_list)
            #xx.append(x12_list)
            #xx.append(x13_list)
            #xx.append(x14_list)
            #xx.append(x15_list)

            yy.append(y1_list)
            yy.append(y2_list)
            yy.append(y3_list)
            yy.append(y4_list)
            yy.append(y5_list)
            yy.append(y6_list)
            yy.append(y7_list)
            #yy.append(y8_list)
            #yy.append(y9_list)
            #yy.append(y10_list)
            #yy.append(y11_list)
            #yy.append(y12_list)
            #yy.append(y13_list)
            #yy.append(y14_list)
            #yy.append(y15_list)

            
            num = 0
            x_title = ['0', 'A', 'T', 'C', 'G', 'AT', 'AC', 'AG', ]
            y_title = ['0', 'A', 'T', 'C', 'G', 'AT', 'AC', 'AG', ]
            fig = plt.figure()
            row = 8
            for y in yy:
                row = row - 1
                col = 0
                for x in xx:
                    num = num + 1
                    col = col + 1
                    print col+row*8
                    ax = plt.subplot(8, 8, col+row*8)
                    ax.set_xticks([])
                    ax.set_yticks([])
                    plt.sca(ax)
                    area = [[0 for i in range(N+1)] for m in range(N+1)]
                    #for i in xrange(len(s_list)-1):
                    #    a = s_list[i]*1.0/s_list[i+1]
                    #    a = round(a,4)
                    #    r_list.append(a)
                    #print r_list
                    for i in xrange(len(x)):
                        try:
                            area[x[i]][y[i]] +=1
                        except Exception, e:
                            print x[i],y[i]
                    p = plt.imshow(area,interpolation='nearest')
                    #plt.hist2d(x, y, bins=[N,N])
                    #plt.colorbar()
                    if(num < 9):
                        plt.xlabel(x_title[num-1], fontsize=6)
                    if(num % 8 == 1):
                        plt.ylabel(y_title[num/8], fontsize=6)
            filename = filename.split('.fa')[0]
            plt.savefig(filename+"_m="+str(N)+".png", dpi=1000)
            plt.close()

            # for i in xrange(len(x_list)):
            #     try:
            #         area[x_list[i]][y_list[i]] +=1
            #     except Exception, e:
            #         print x_list[i],y_list[i]
            # filename = filename.split("\\")[-1]
            # filename = filename.split('.fa')[0]
            # csvfile = filename+'.csv'
            # with open(csvfile, 'wb') as csvfile:
            #     fieldnames = ['X', 'Y','Count']
            #     writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
            #     for x in range(N):
            #         for y in range(N):
            #             writer.writerow({'X':x,'Y':y,'Count':area[x][y]})       
            # #print r_list[:10]
            f.close()

